﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YamlDotNet.Serialization;

namespace Merge
{
    public static class Utils
    {
        private static Regex reg_Nums = new Regex(@"[\d\.]+", RegexOptions.Compiled);
        private static Regex reg_0d = new Regex(@"0\.?\d", RegexOptions.Compiled);
        public static bool FileNameContainKeyword(string fileName, string keyword, bool fuzzyDot)
        {
            keyword = keyword.Trim();
            float f;
            string[] numsStrings = reg_Nums.Matches(fileName).OfType<Match>().Select(x => x.Value.Trim()).ToArray();
            if (fuzzyDot && reg_0d.IsMatch(keyword))
            {
                string d = keyword.Replace("0.", "0");
                return numsStrings.Select(x => x.Replace("0.", "0")).Any(x => x == d);
            }
            else if (float.TryParse(keyword, out f))
            {
                return reg_Nums.Matches(fileName).OfType<Match>().Select(x => x.Value.Trim()).Any(x => x == keyword);
            }
            else
            {
                return fileName.Contains(keyword);
            }
        }

        public static bool WithInFilter(Filters filters, Cell cell, IMergeSettings settings)
        {
            //TODO: not dup linq each time
            //var selectedFilters = filters.AllFilters.Where(x => x.Selected).OrderBy(x => x.SortingNumber);

            foreach (Filter filter in filters.AllFilters.Where(x => x.Selected).OrderBy(x => x.SortingNumber))
            {
                if (filter.IncludeKeywords.All(x => Utils.FileNameContainKeyword(cell.TidyFileName, x, settings.FuzzyMatchDot))
                    && !filter.FinalExcludeKeywords.Any(x => Utils.FileNameContainKeyword(cell.TidyFileName, x, false)))
                {
                    cell.FilterName = filter.Name;
                    //cell.FilterOrder = filter.SortingNumber;
                    return true;
                }
            }
            return false;
        }

        private static Regex regCopyCount = new Regex(@"([0-9\.]+)\s*(?:件|套)", RegexOptions.Compiled);
        private static Regex regCopyCountZh = new Regex(@"(?<![一二三四五六七八九十])([一二三四五六七八九十])\s*(?:件|套)", RegexOptions.Compiled);
        public static string ExtractCopyCounts(string dxfName, bool showQuestionMarkOnFailure)
        {
            if (regCopyCount.IsMatch(dxfName))
                return regCopyCount.Match(dxfName).Groups[1].Value;
            else if (regCopyCountZh.IsMatch(dxfName))
                return regCopyCountZh.Match(dxfName).Groups[1].Value;
            else
                return showQuestionMarkOnFailure ? "？" : string.Empty;
        }
        private static readonly List<string> chineseChars = new List<string> { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
        public static string ExtractCopyCountsToIntStr(string dxfName, bool showQuestionMarkOnFailure)
        {
            string s = ExtractCopyCounts(dxfName, showQuestionMarkOnFailure);
            if (chineseChars.Contains(s))
            {
                return (chineseChars.IndexOf(s) + 1).ToString();
            }
            return s;
        }
    }
}
